import plugin from '../../lib/plugins/plugin.js'
import YAML from "yaml"
import fs from "fs"

let file = `config/config/group.yaml`;
export class jinyong extends plugin {
    constructor() {
        super({
            name: "前缀开关",
            dsc: "控制机器人在指定群开关",
            event: "message",
            priority: 1000, //指令优先级
            rule: [
			{
			    reg: '^#?(查看|设置|新增|删除)前缀(.*)$', 
                fnc: "setqian",
            },
			{
                reg: '^#?(开启|关闭)(主人)?前缀$', 
                fnc: "onqian",
            },
			{
                reg: '^#?(开启|关闭)默认(主人)?前缀$', 
                fnc: "onallqian",
            }
           ]
        });
    }


  async setqian(e) {
    if (!e.isMaster) return;
	
    let ids = `${this.e.self_id}:default`;
    let data = YAML.parse(fs.readFileSync(file, 'utf8'));
	if (!data[ids]) {
		data[ids] = {}
	}
	
    if (e.msg.includes('查看前缀')) {
		if (data[ids].botAlias.length) {
            e.reply(data[ids].botAlias.join('，'))
		}
    } 
	else if (e.msg.includes('设置前缀')) {
        let key = e.msg.replace(/#|设置前缀/g, '').trim();
        data[ids].botAlias = []
		data[ids].botAlias.push(JSON.parse(key))
		await fs.writeFileSync(file, YAML.stringify(data));
		e.reply(`${this.e.self_id}前缀已设置 ${key}`,false,{ recallMsg: 10 })
	}
	else if (e.msg.includes('新增前缀')) {
		let key = e.msg.replace(/#|新增前缀/g, '').trim();
		data[ids].botAlias.push(JSON.parse(key))
		await fs.writeFileSync(file, YAML.stringify(data));
		e.reply(`${this.e.self_id}前缀已新增 ${key}`,false,{ recallMsg: 10 })
	}
	else if (e.msg.includes('删除前缀')) {
        let key = e.msg.replace(/#|删除前缀/g, '').trim();
		let index = data[ids].botAlias.indexOf(key);
        if (index !== -1) {
            data[ids].botAlias.splice(index, 1); // 从数组中删除前缀
            await fs.writeFileSync(file, YAML.stringify(data));
            e.reply(`${this.e.self_id}前缀已删除 ${key}`, false, { recallMsg: 10 });
        } else {
            e.reply(`${this.e.self_id}前缀 ${key} 不存在`, false, { recallMsg: 10 });
        }
    }
    return true;
}



  async onqian(e) {
	if (!e.isGroup) return
	if (!e.isMaster && !e.member.is_owner && !e.member.is_admin) {
		logger.info(`${e.user_id} 无权限操作`)
		return
	}

	let ids = `${this.e.self_id}:${e.group_id}`
    let data = YAML.parse(fs.readFileSync(file, 'utf8'));
	if (!data[ids]) {
		data[ids] = {}
	}
	
	if (e.msg.includes('关闭')) {
		data[ids].onlyReplyAt = 0
		await fs.writeFileSync(file, YAML.stringify(data));
		e.reply(`群前缀已关闭`,false,{ recallMsg: 10 })
	}
	else {
		if (e.msg.includes(`主人`)) {
		    data[ids].onlyReplyAt = 2
			await fs.writeFileSync(file, YAML.stringify(data));
			e.reply(`群非主人前缀已开启`,false,{ recallMsg: 10 })
		} else {
			data[ids].onlyReplyAt = 1
			await fs.writeFileSync(file, YAML.stringify(data));
			e.reply(`群前缀已开启`,false,{ recallMsg: 10 })
		}
	}
	return true	
  }


  async onallqian(e) {
	if (!e.isMaster) return

    let ids = `${this.e.self_id}:default`
	let data = YAML.parse(fs.readFileSync(file, 'utf8'));
	if (!data[ids]) {
		data[ids] = {}
	}
	if (e.msg.includes('关闭')) {
		data[ids].onlyReplyAt = 0
		await fs.writeFileSync(file, YAML.stringify(data));
		e.reply(`默认前缀已关闭`,false,{ recallMsg: 10 })
	}
	else {
		if (e.msg.includes(`主人`)) {
		    data[ids].onlyReplyAt = 2
			await fs.writeFileSync(file, YAML.stringify(data));
			e.reply(`默认主人前缀已开启`,false,{ recallMsg: 10 })
		} else {
			data[ids].onlyReplyAt = 1
			await fs.writeFileSync(file, YAML.stringify(data));
			e.reply(`默认前缀已开启`,false,{ recallMsg: 10 })
		}
	}
	return true
  }
	
}